var ScriptReader = {};


/*----------------------------------------------------------------------*/
/* INIT */
ScriptReader.Init = function () {
	if (parent.location.href != self.location.href) {
		var parentDlg_PleaseWait = $("#dlgPreviewScript_PleaseWait", parent.document);
		var parentDlg_Content = $("#dlgPreviewScript_content", parent.document);
		if (parentDlg_PleaseWait.length > 0) {
			parentDlg_Content.removeClass("hidden");
			parentDlg_PleaseWait.fadeOut("fast");

		}
		else {
			var parentScriptPanelIframe = $("#scriptPanel-Script-ScriptContainer", parent.document);
			if (parentScriptPanelIframe.length > 0) {
				parentScriptPanelIframe.show();
				parentScriptPanelIframe.siblings("#scriptPanel-Script-PleaseWait").hide();
			}
		}


	}
};

/*----------------------------------------------------------------------*/
/* INITHANDLERS */
ScriptReader.InitHandlers = function () {
    /* .scriptDecision click */
    /*--------------------------*/
    $(".scriptDecision > A").live("click", function(e){
        var scriptBlock = $(this).siblings(".scriptBlock");
        
        if(scriptBlock.is(":visible")){
            scriptBlock.slideUp(100);
        }
        else{
            if(scriptBlock.html() == ""){
            	ScriptReader.LoadDecision(scriptBlock);
            }
            scriptBlock.slideDown(100);
        }
        
        e.stopImmediatePropagation();
    });

};
/* END INITHANDLER */

/*----------------------------------------------------------------------*/
/* LOAD EXISTING DECISION */
ScriptReader.LoadDecision = function (scriptBlock) {
	var scriptBlockID = 	scriptBlock.attr("scriptblockid");
	var scriptDecision = 	scriptBlock.parent();

	var onSuccess = function(response){
		if(!response.Success){
			alert("Failed to load Script. Please try clicking on this Decision again.\n If loading continues to fail then please report it.\n\nError: " + response.ErrorMessage);
			return false;
		}
		
		var decisionID = 	    response.Data.DecisionID;
		var scriptBlockID = 	response.Data.ScriptBlockID;
		var scriptText = 	    response.Data.ScriptText;
		var decisions =         response.Data.Decisions;

		var template = ScriptReader.BuildScriptBlockHTML(decisionID, scriptBlockID, scriptText, decisions)
		var newScriptBlock = $(template);
		
		scriptBlock.remove();
		scriptDecision.append(newScriptBlock);
		newScriptBlock.removeClass("hidden");
		
	};
	/* end onSuccess */

	var onError = function(jqXHR, textStatus, errorThrown){
		alert("Failed to load Script. Please try collapsing and expanding this Decision again.\n If loading continues to fail then please report it.\n\nError: " + errorThrown);
	};
	
	var onComplete = function(jqXHR, textStatus){
	};
	
	
	$.ajax({
		url:		"AJAX/ScriptReader.asp?method=GetScriptBlockAndDecisions",
		type: 		"POST",
		success: 	onSuccess,
		error: 		onError,
		complete: 	onComplete,
		data:		{
						ScriptBlockID: scriptBlockID
					}
	});	
};

/*----------------------------------------------------------------------*/
/* BUILD SCRIPT BLOCK HTML */
ScriptReader.BuildScriptBlockHTML = function (ScriptBlockID, ParentDecisionID, ScriptText, ScriptDecisions) {
	var template = $("#scriptBlockTemplate").html();
	
	template = template.replace(/\{ScriptBlockID\}/gi, ScriptBlockID);
	template = template.replace(/\{ParentDecisionID\}/gi, ParentDecisionID);
	template = template.replace(/\{ScriptText\}/gi, ScriptText);
	
	var decisionHTML = [];
	for(var x = 0, len = ScriptDecisions.length; x < len; x++){
	    var curDecision = ScriptDecisions[x];
	    var html = ScriptReader.BuildDecisionHTML(curDecision.DecisionID, curDecision.ChildScriptBlockID, curDecision.DecisionTitle);
	    
	    decisionHTML.push(html);
	}
	
	template = template.replace(/\{ScriptDecisions\}/gi, decisionHTML.join(""));
	
	return template;
}

/*----------------------------------------------------------------------*/
/* BUILD DECISION HTML */
ScriptReader.BuildDecisionHTML = function (DecisionID, ChildScriptBlockID, DecisionTitle) {
	var template = $("#decisionTemplate").html();

	template = template.replace(/\{DecisionID\}/gi, DecisionID);
	template = template.replace(/\{ChildScriptBlockID\}/gi, ChildScriptBlockID);
	template = template.replace(/\{DecisionTitle\}/gi, DecisionTitle);
	
	return template;
}

/*----------------------------------------------------------------*/
$(document).ready(function(){
	ScriptReader.Init();
	ScriptReader.InitHandlers();
});